using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class admin_PackageBookingManagement : admin_BaseUserControl
{
    PackageBooking pb = new PackageBooking();
    protected void Page_Load(object sender, EventArgs e)
    {
        ActiveAccording = "booking";
        if (Request["id"] == null) return;
        try
        {            
            int id = int.Parse(Request["id"]);
            CurrentID = Request["id"];
            Person per = new Person();
            Booking bk = new Booking();
            BookingDAO daoBooking = new BookingDAO();
            PackageBookingDAO daoPackageBook = new PackageBookingDAO();
            pb = daoPackageBook.GetPackageBookingByBID(id);
            bk = daoBooking.GetBooking(pb.BookingID);
            pb.BookingID = bk.BookingID;
            //get personal information
            PersonDAO daoPerson = new PersonDAO();
            per = daoPerson.GetPerson(bk.CustomerID);
            lblCustomerName.Text = per.FirstName + ' ' + per.LastName;
            lblEmail.Text = per.Email;
            lblPhone.Text = per.Phone;
            lblAddress.Text = per.Address;
            txtNote.Text = pb.Note;
          
            txtDepartureDate.Text = pb.DepartureTime.ToShortDateString();
            txtGuestNumber.Text = pb.GuestNumber.ToString();          
            //get tour information
            TourDAO daoTour = new TourDAO();
            Tour tour = daoTour.GetTour(pb.Tour.Id);
            lblPackage.Text = tour.Name;
            cmbState.Items.Add(new ListItem("Booked","0"));
            cmbState.Items.Add(new ListItem("Accepted", "1"));
            cmbState.Items.Add(new ListItem("Canceled", "2"));
            cmbState.SelectedIndex = (int)bk.State;
        }
        catch (Exception ex)
        {
            ShowMessage("Error");
        }
    }
    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        if (pb.ID <= 0)
        {
            ShowMessage("Error when try to update information");
            return;
        }
        BookingDAO daoBooking = new BookingDAO();
        PackageBookingDAO daoPB=new PackageBookingDAO();
        pb.DepartureTime = DateTime.Parse(txtDepartureDate.Text);
        pb.GuestNumber = int.Parse(txtGuestNumber.Text);
        pb.Note = txtNote.Text;
        byte state = byte.Parse(cmbState.SelectedValue);
        if (daoPB.Update(pb) > 0)
        {
            if (daoBooking.UpdateState(pb.BookingID, state) > 0)
            {
                ShowMessage("Booking details is updated");
            }
            else ShowMessage("Error. The change cannot be updated");
        }
        else ShowMessage("Error. The change cannot be updated");
    }
    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        BookingDAO daoBooking = new BookingDAO();
        if (daoBooking.Delete(pb.BookingID) > 0)
        {
            Response.Redirect("Default.aspx?ctrl=booking_list");
        }
        else ShowMessage("Error. The information cannot be deleted");
    }

    /// <summary>
    /// Return request string for search in list page
    /// </summary>
    public string GetSearchRequest()
    {
        if (Request["search"] != null) return Request["search"];
        return "";
    }
}
